﻿using MDIApplication.Classes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDIApplication
{
    public partial class UpdateRoomInfo : Form
    {
        public UpdateRoomInfo()
        {
            InitializeComponent();
            
        }

        private void UpdateRoomInfo_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'updateRoomInfoDataSet.Room' table. You can move, or remove it, as needed.
            this.roomTableAdapter.Fill(this.updateRoomInfoDataSet.Room);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.dataGridView1.EndEdit();
            this.dataGridView1.RefreshEdit();

            //ArrayList selectedRoomList = new ArrayList();
            //Room ro = new Room();
            //ro.roomUnitNo = Convert.ToInt32(dataGridView1[1, i].Value);
            //ro.roomLevel = Convert.ToInt32(dataGridView1[2, i].Value);
            //ro.roomType = (string)dataGridView1[3, i].Value;
            //ro.roomStatus = (string)dataGridView1[4, i].Value;
            //selectedRoomList.(ro);
            //string roomListString = "datasource=localhost;port3306;username = sa'password=imsa";
            //MySqlConnection conDataBase = new MySqlConnection(roomListString);

            //try
            //{
            //    MySqlDataAdapter sda = new MySqlDataAdapter();
            //    BindingSource roomListbSource = new BindingSource();
            //    roomListbSource.DataSource = dbdataset;
            //    dataGridView1.DataSource = roomListbSource;
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            
            //}
        }

        #region toolStrip
        UpdateRoomInfo updateRoomInfoChild;
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (updateRoomInfoChild == null)
            {
                updateRoomInfoChild = new UpdateRoomInfo();
                updateRoomInfoChild.MdiParent = this.MdiParent;
                updateRoomInfoChild.Location = new Point(0, 0);
                // Display the form.
                updateRoomInfoChild.Show();
                updateRoomInfoChild.Activate();
            }

        }

        SearchForStaffHousekeepDuty dutyAllocationChild;
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (dutyAllocationChild == null)
            {
                dutyAllocationChild = new SearchForStaffHousekeepDuty();
                dutyAllocationChild.MdiParent = this.MdiParent;
                dutyAllocationChild.Location = new Point(0, 0);
                // Display the form.
                dutyAllocationChild.Show();
                dutyAllocationChild.Activate();
            }
        }

        SearchForStaff updateStaffChild;
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (updateStaffChild == null)
            {
                updateStaffChild = new SearchForStaff();
                updateStaffChild.MdiParent = this.MdiParent;
                updateStaffChild.Location = new Point(0, 0);
                // Display the form.
                updateStaffChild.Show();
                updateStaffChild.Activate();
            }
        }
        #endregion toolStrip

        #region CloseAllForms
        private void CloseAllForms()
        {
            Form[] formToClose = null;
            int i = 1;
            foreach (Form form in Application.OpenForms)
            {
                if (form != this && form.IsMdiChild) //this is form1
                {
                    Array.Resize(ref formToClose, i);
                    formToClose[i - 1] = form;
                    i++;
                }
            }
            if (formToClose != null)
            {
                for (int j = 0; j < formToClose.Length; j++)
                {
                    formToClose[j].Close();
                }
                updateRoomInfoChild = null;
                dutyAllocationChild = null;
                updateStaffChild = null;
            }
        }
        #endregion CloseAllForms

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                Room r = new Room();
                r.roomUnitNo = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
                r.roomLevel=Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[1].Value);
                r.roomType=Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[2].Value);
                r.roomStatus=Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[3].Value);
                BookingRoomAvailabilityDBManager db = new BookingRoomAvailabilityDBManager();
                db.updateRoom(r);
                MessageBox.Show("Room Information Updated");
            }           
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                dataGridView1.BeginEdit(true);
            }    
        }
    }
}
